package com.ssr.master.common.config;

import com.google.common.collect.Maps;
import com.ssr.master.common.utils.PropertiesLoader;
import com.ssr.master.common.utils.StringUtils;
import org.springframework.core.io.DefaultResourceLoader;

import java.io.File;
import java.io.IOException;
import java.util.Map;

/**
 * @author ZhangPeng
 * @Describe //todo
 * @date
 * @updateBy
 * @other
 **/

public class Global {

    private static Global global = new Global();
    private static Map<String, String> map = Maps.newHashMap();
    private static PropertiesLoader loader = new PropertiesLoader(new String[]{"ssr.properties"});
    public static final String SHOW = "1";
    public static final String HIDE = "0";
    public static final String YES = "1";
    public static final String NO = "0";
    public static final String TRUE = "true";
    public static final String FALSE = "false";
    public static final String VIS_OUT = "0";
    public static final String VIS_IN = "1";
    public static final String USERFILES_BASE_URL = "/userfiles/";

    public Global() {
    }

    public static String getPrintPath() {
        return getConfig("printPath");
    }

    public static Global getInstance() {
        return global;
    }

    public static String getConfig(String key) {
        String value = (String)map.get(key);
        if (value == null) {
            value = loader.getProperty(key);
            map.put(key, value != null ? value : "");
        }

        return value;
    }

    public static String getAdminPath() {
        return getConfig("adminPath");
    }

    public static String getFrontPath() {
        return getConfig("frontPath");
    }

    public static String getUrlSuffix() {
        return getConfig("urlSuffix");
    }

    public static Boolean isDemoMode() {
        String dm = getConfig("demoMode");
        return "true".equals(dm) || "1".equals(dm);
    }

    public static Object getConst(String field) {
        try {
            return Global.class.getField(field).get((Object)null);
        } catch (Exception var2) {
            return null;
        }
    }



    public static String getProjectPath() {
        String projectPath = getConfig("projectPath");
        if (StringUtils.isNotBlank(projectPath)) {
            return projectPath;
        } else {
            try {
                File file = (new DefaultResourceLoader()).getResource("").getFile();
                if (file != null) {
                    while(true) {
                        File f = new File(file.getPath() + File.separator + "src" + File.separator + "main");
                        if (f == null || f.exists() || file.getParentFile() == null) {
                            projectPath = file.toString();
                            break;
                        }

                        file = file.getParentFile();
                    }
                }
            } catch (IOException var3) {
                var3.printStackTrace();
            }

            return projectPath;
        }
    }

    public static String[] getLogOutList() {
        String urls = getConfig("sso.logout.urls");
        return urls.split(",");
    }

    public static String getLogin() {
        String urls = getConfig("sso.login.url");
        return urls;
    }
}
